Advent of Code - Day 9: Rope Bridge

9h day of the advent of code using R

advent of code
Published

December 9, 2022

Part 1

Input

sample <-   readLines("sample.txt")
input <- readLines("input.txt")

head(input, 10)
 [1] "U 2" "D 2" "R 2" "U 2" "D 1" "L 2" "R 2" "D 1" "R 2" "D 1"

Part 1

Code

move_knot <- function(head_coord, knot_coord) {
  
  # if the head and tail aren't touching and aren't in the same row or column
  if (abs(head_coord$x - knot_coord$x) >  1 | abs(head_coord$y - knot_coord$y) > 1) {
    available_coord <- list(
      list(x = knot_coord$x + 1, y = knot_coord$y),
      list(x = knot_coord$x + 1, y = knot_coord$y + 1),
      list(x = knot_coord$x + 1, y = knot_coord$y - 1),
      list(x = knot_coord$x - 1, y = knot_coord$y),
      list(x = knot_coord$x - 1, y = knot_coord$y + 1),
      list(x = knot_coord$x - 1, y = knot_coord$y - 1),
      list(x = knot_coord$x, y = knot_coord$y + 1),
      list(x = knot_coord$x, y = knot_coord$y - 1),
      list(x = knot_coord$x, y = knot_coord$y)
    )
    
    # Coordinate difference are computed between head and all possible future
    # knot position
    coord_diff <- unlist(
      purrr::map(available_coord,
                 ~ abs(.x$x - head_coord$x) + abs(.x$y - head_coord$y)))
    
    
    # new knot coordinate is the one with lowest difference
    knot_coord <- available_coord[[which(coord_diff == min(coord_diff))]]
  }
  return(knot_coord)
}
head_coord <- list(x=0,y=0)
tail_coord <- list(x=0,y=0)

visited_coord <- c()

for (move in input) {
  
  move_vec <- unlist(strsplit(move, " "))
  
  direction <- move_vec[1]
  distance <- move_vec[2]
  
  for (d in 1:distance) {
    # message(d)
    if (direction == "R") {
      head_coord$x <- head_coord$x + 1
    } else if (direction == "L"){
      head_coord$x = head_coord$x - 1
    } else if (direction == "U") {
      head_coord$y = head_coord$y + 1
    } else if (direction == "D") {
      head_coord$y = head_coord$y - 1
    }
    
    tail_coord <- move_knot(head_coord = head_coord, knot_coord = tail_coord)
    
    visited_coord <- c(visited_coord, paste(tail_coord$x, tail_coord$y, sep =  ";"))
  }
}

Result

length(unique(visited_coord))
[1] 6337

Part 2

knots <- list()

for (i in 1:10) {
  knots[[i]] <- list(x=0, y=0)
}


visited_coord <- c()

for (move in input) {
  
  move_vec <- unlist(strsplit(move, " "))
  
  direction <- move_vec[1]
  distance <- move_vec[2]
  
  for (d in 1:distance) {
    if (direction == "R") {
      knots[[1]]$x <- knots[[1]]$x + 1
    } else if (direction == "L"){
      knots[[1]]$x = knots[[1]]$x - 1
    } else if (direction == "U") {
      knots[[1]]$y = knots[[1]]$y + 1
    } else if (direction == "D") {
      knots[[1]]$y = knots[[1]]$y - 1
    }
    for (i in 1:9) {
      new_coord <- move_knot(knots[[i]], knots[[i+1]])
      if (new_coord$x != knots[[i+1]]$x | new_coord$y != knots[[i+1]]$y) {
        knots[[i+1]] <- new_coord
      } else{
        break
      }
      
    }
    visited_coord <- c(visited_coord, paste(knots[[10]]$x, knots[[10]]$y,sep = ";"))
  }
  
}

Result

length(unique(visited_coord))
[1] 2455